﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MaxSMART;

namespace GetSMART {
    /// <summary>
    /// DriveWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DriveWindow : Window {
        readonly SmartDrive Drive;

        public DriveWindow(SmartDrive drive) {
            Drive = drive;
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            DrivesList.ItemsSource = new[] { Drive };
            UpdateData();
        }

        class SmartData {
            public SmartMethod Method { get; set; }
            public byte Index { get; set; }
            public bool IdentifyOK { get; set; }
            public string Serial { get; set; }
            public string Model { get; set; }
            public bool EnableOK { get; set; }
            public bool AttrOK { get; set; }
            public byte Temperature { get; set; }
            public bool ThresOK { get; set; }
            public byte TempThres { get; set; }
        }

        void UpdateData() {
            DataList.ItemsSource = null;

            if (Drive != null) {
                var dataList = new List<SmartData>();
                foreach (SmartMethod method in Enum.GetValues(typeof(SmartMethod))) {
                    for(byte i = 0; i < 4; i++) {
                        var data = new SmartData() { Method = method, Index = i };
                        Drive.Index = i;
                        
                        Drive.Serial = null;
                        Drive.Model = null;
                        data.IdentifyOK = Drive.ExecuteCommand(method, SmartCommand.IdentifyDevice);
                        if (data.IdentifyOK) {
                            data.Serial = Drive.Serial;
                            data.Model = Drive.Model;
                        }
                        
                        if (method != SmartMethod.Storage) data.EnableOK = Drive.ExecuteCommand(method, SmartCommand.EnableSmart);
                        
                        Drive.Attributes.Clear();
                        data.AttrOK = Drive.ExecuteCommand(method, SmartCommand.ReadAttributes);
                        if (data.AttrOK) {
                            SmartAttributeData attr;
                            if (Drive.Attributes.TryGetValue(SmartAttribute.Temperature, out attr)) {
                                data.Temperature = attr.Value;
                            }
                        }

                        if (method != SmartMethod.Storage) { // not supported
                            Drive.Thresholds.Clear();
                            data.ThresOK = Drive.ExecuteCommand(method, SmartCommand.ReadThresholds);
                            if (data.ThresOK) {
                                byte thres;
                                if (Drive.Thresholds.TryGetValue(SmartAttribute.Temperature, out thres)) {
                                    data.TempThres = thres;
                                }
                            }
                        }

                        dataList.Add(data);
                        if (method == SmartMethod.Storage) break; // DriveHead is irrelevant for Storage method.
                    }
                }
                DataList.ItemsSource = dataList;
            }
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e) {
            UpdateData();
        }
    }
}
